<import template="/admin/_header"/>
<import template="/admin/_content"/>

<div class="page-title">
    <div class="title-env">
        <h1 class="title">邀请码管理</h1>
        <p class="description">邀请码列表！</p>
    </div>
    <div class="breadcrumb-env">
        <ol class="breadcrumb bc-1">
            <li><a href="/admin"><i class="fa-home"></i>管理端</a></li>
            <li><a href="/admin/invite">邀请码</a></li>
        </ol>
    </div>
</div>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="panel panel-default">

                    <div class="panel-heading">
                        <h3 class="panel-title"></h3>

                        <div class="panel-options">
                            <a href="javascript:;" id="invite-add"><span>+</span></a>
                            <a href="/admin/invite/export" title="导出到Excel.." target="_blank"><i class="fa-file-excel-o"></i></a>
                            <a href="javascript:;" data-toggle="reload">
                                <i class="fa-rotate-right"></i>
                            </a>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table cellspacing="0" class="table table-small-font table-bordered table-striped">
                                <tbody>
                                <tr>
                                    <th>UID</th>
                                    <th>添加时间</th>
                                    <th>邀请码</th>
                                    <th>等级</th>
                                    <th>状态</th>
                                    <th width="150px">操作</th>
                                </tr>
                                <if condition="count($inviteList) > 0">
                                    <loop variable="$inviteList" key="$key" value="$invite">
                                        <tr id="invite-{$invite->id}">
                                            <td>#{$invite->uid}</td>
                                            <td>{(date("Y-m-d H:i:s", $invite->dateLine))}</td>
                                            <td> {$invite->invite}</td>
                                            <td>{(\Helper\Utils::planAutoShow($invite->plan))}</td>
                                            <td>
                                                <if condition="$invite->status == 1">已用
                                                    <else>未用
                                                </if>
                                            </td>
                                            <td>
                                                <a class="btn btn-info btn-sm" href="javascript:;"
                                                   onclick="inviteModify('{$invite->id}');">编辑</a>
                                                <a class="btn btn-danger btn-sm" href="javascript:;"
                                                   onclick="inviteRemove('{$invite->id}');">删除</a>
                                            </td>
                                        </tr>
                                    </loop>
                                    <else>
                                        <tr id="invite-0">
                                            <td>#</td>
                                            <td colspan="5">没有邀请码</td>
                                        </tr>
                                </if>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
    </div>
</section>
</div>
</div>
<!-- Modal 6 (Long Modal)-->
<div class="modal fade" id="modal-6">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"></h4>
            </div>
            <form role="form" id="form1" method="post" action="/admin/invite/update.json" class="validate">
                <input type="hidden" name="form_add" value="{(time())}"/>
                <input type="hidden" name="invite" id="invite" value=""/>
                <div class="modal-body">
                    <div id="modal-isload">Content is loading...</div>
                    <div id="modal-isbody" style="display: none;">
                        <div class="row" id="add_select_uid" style="display:none;">
                            <div class="col-md-12">
                                <div class="input-group">
                                    <span class="input-group-btn" id="selectList">
                                        <button type="button" class="btn dropdown-toggle" id="user-select" data-toggle="dropdown">
                                            公共邀请码 <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li><a href="javascript:;" data-selectid="-1">公共邀请码</a></li>
                                            <li><a href="javascript:;" data-selectid="{$user->uid}">私有邀请码</a></li>
                                            <li class="divider"></li>
                                            <li><a href="javascript:;" data-selectid="0">给指定UID</a></li>
                                        </ul>
                                    </span>
                                    <input type="text" name="add_uid" id="field-0" value="-1" class="form-control no-left-border form-focus-red" title="邀请码所属用户(默认-1公共,指定UID为私密)" placeholder="邀请码所属用户(默认-1公共)">
                                </div>
                                <hr/>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">

                                <div class="form-group">
                                    <label for="field-1" class="control-label">邀请码（只允许系统生成）</label>

                                    <input type="text" class="form-control" name="invite_2" id="field-1" value=""
                                           readonly placeholder="只允许系统生成">
                                </div>

                            </div>

                            <div class="col-md-6">

                                <div class="form-group">
                                    <label for="field-2" class="control-label">邀请码类型</label>

                                    <select name="plan" id="plan" id="field-2" class="form-control">
                                        <loop variable="$planList" key="$key" value="$plan">
                                            <option value="{$key}">({$key}){$plan}</option>
                                        </loop>
                                    </select>
                                </div>

                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">

                                <div class="form-group">
                                    <label for="field-3" class="control-label">有效时间(天)</label>

                                    <input type="number" class="form-control" name="expiration" id="field-3" value="10"
                                           placeholder="10" data-message-required="有效时间为整数.">
                                </div>

                            </div>

                            <div class="col-md-6">

                                <div class="form-group">
                                    <label for="field-4" class="control-label">添加时间</label>

                                    <div class="date-and-time">
                                        <input type="text" id="field-4" class="form-control datepicker"
                                               placeholder="{(date('Y-m-d', time()))}" name="add_time1"
                                               data-format="yyyy-mm-dd">
                                        <input type="text" class="form-control timepicker" name="add_time2"
                                               data-template="dropdown" data-show-seconds="true"
                                               data-default-time="11:25" data-show-meridian="false" data-minute-step="5"
                                               data-second-step="5"/>
                                    </div>
                                </div>

                            </div>
                        </div>

                        <div class="row invite-num">
                            <div class="col-md-6">

                                <div class="form-group">
                                    <label for="field-5" class="control-label">添加邀请码个数</label>

                                    <input type="number" class="form-control" name="number" id="field-5" value="1"
                                           placeholder="1" data-message-required="邀请码个数.">
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-success">立即保存</button>
                </div>
            </form>
        </div>

    </div>
</div>
<script type="text/javascript">
    var modal_default_body = jQuery('#modal-6 .modal-body').html();
    jQuery(document).ready(function($) {

        $("form#form1").validate({
            rules: {
                'user.email': "required",
                'user.port': "required",
                'user.sspwd': "required",
                'user.invite_num': "required",
                'user.transfer': "required"
            },
            messages: {
                'user.email': "邮箱必须填写",
                'user.port': "端口必须填写",
                'user.sspwd': "SS连接密码必须填写",
                'user.invite_num': "邀请码数量必须填写",
                'user.transfer': "总流量必须填写"
            },
            errorElement: 'span',
            errorClass: 'validate-has-error',
            highlight: function (element) {
                $(element).closest('.form-group').addClass('validate-has-error');
            },
            unhighlight: function (element) {
                $(element).closest('.form-group').removeClass('validate-has-error');
            },
            errorPlacement: function (error, element)
            {
                if(element.closest('.has-switch').length)
                {
                    error.insertAfter(element.closest('.has-switch'));
                }
                else {
                    if(element.parent('.checkbox, .radio').length || element.parent('.input-group').length)
                    {
                        error.insertAfter(element.parent());
                    }
                    else
                    {
                        error.insertAfter(element);
                    }
                }
            },
            submitHandler: function(form) {
                $.ajax({
                    url: form.action,
                    method: "POST",
                    dataType: "json",
                    data: $(form).serialize(),
                    success: function(result){
                        if(result.code != 200) {
                            showToastr(result.message, 0, 3000, '系统错误');
                            return;
                        }
                        if(result.data.error == 0) {
                            $('#modal-6').modal('hide');
                            location.reload();
                        }
                        showToastr(result.data.message, 0);

                    },
                    error: function(Xhr){
                        showToastr("出现未知错误，请重试", 0);
                    }
                });
            }
        });

        $("#invite-add").on('click', function(){
            $("#modal-6 .modal-body").html(modal_default_body);
            $("input[name='number']").show().val('');
            $("input[name='invite']").val('');
            $("input[name='invite_2']").val('');
            $("#plan").val('A');
            $("input[name='expiration']").val('');
            $("input[name='add_time1']").val('');
            $('#modal-6 .modal-title').html('新增邀请码');
            $('#modal-6').modal('show', {backdrop: 'static'});
            reset(true);
            $("#add_select_uid").show();
            $("#selectList a").on('click', function (){
                var defVal = ' <span class="caret"></span>';
                var selectVal = $(this).text();
                $("#user-select").html(selectVal + defVal);
                $("input[name='add_uid']").val($(this).attr('data-selectid'));
            });
        });
    });

    function inviteRemove(inviteId) {
        $.ajax({
            url: "/admin/invite/delete.json",
            method: "POST",
            dataType: "json",
            data: {id: inviteId},
            success: function(result){
                if(result.code != 200){
                    showToastr(result.message, 0, 3000, '系统错误');
                    return;
                }
                if(result.data.error == 0) {
                    $("#invite-" + inviteId).remove();
                }
                showToastr(result.data.message, 0);
            },
            error: function(Xhr){
                showToastr("出现未知错误，请重试或联系管理员", 0);
            }
        });
    }
    function inviteModify(inviteId) {
        $("#modal-6 .modal-body").html(modal_default_body);
        reset(false);
        $.ajax({
            url: "/admin/invite/query.json",
            method: "POST",
            dataType: "json",
            data: {id: inviteId},
            success: function (result) {
                if(result.code != 200){
                    $('#modal-6').modal('hide');
                    showToastr(result.message, 0, 3000, '系统错误');
                    return;
                }

                if(result.data.error != 1 && result.data.invite != null) {
                    var invite = result.data.invite;
                    $("input[name='invite']").val(invite.invite);
                    $("input[name='invite_2']").val(invite.invite);
                    $("#plan").val(invite.plan);
                    $("input[name='expiration']").val(invite.expiration);
                    $("input[name='add_time1']").val(invite.dateLine);
                    $("input[name='number']").hide().val('');
                    $(".invite-num").remove();
                    $('#modal-6 .modal-title').html('修改邀请码');
                    reset(true);
                }
            },
            error: function (Xhr) {
                showToastr("出现未知错误，请重试或联系管理员", 0);
            }
        });
        $('#modal-6').modal('show', {backdrop: 'static'});
    }
</script>
<import template="/admin/_footer"/>